package com.example.dangerouscargodriver.ui.fragment.tab;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.baiju.netmanager.Http.IHttpRequest;
import com.example.dangerouscargodriver.R;
import com.example.dangerouscargodriver.adapter.OrderInfoAdapter;
import com.example.dangerouscargodriver.controller.AuthController;
import com.example.dangerouscargodriver.entry.OrderStatusInfo;
import com.example.dangerouscargodriver.module.ResponseInfo;
import com.example.dangerouscargodriver.param.RemoteAPI;
import com.example.dangerouscargodriver.param.RemoteAPICmd;
import com.example.dangerouscargodriver.ui.fragment.HttpRequestFragment;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;

public class OrderManagerTabDiscussFragment extends HttpRequestFragment {
    private static final String TAG = "OrderManagerTabDiscuss";
    public static int TAB_ENUM_ALL = 0;
    public static int TAB_ENUM_DISCUSSING = 5;
    public static int TAB_ENUM_TRANSPORTING = 2;
    public static int TAB_ENUM_DONE = 3;
    public static int TAB_ENUM_CANCEL = 9;
    public static int TAB_ENUM_LOSE_EFFICACY = 4;

    private View mRootView = null;
    private LinearLayout mEmptyLayout = null;
    private List<OrderStatusInfo> mOrderInfo = null;
    private OrderInfoAdapter mAdapter = null;
    private RecyclerView mOrderList = null;

    public OrderManagerTabDiscussFragment(){
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.initHttpComponent(OrderManagerTabDiscussFragment.class);
        mAdapter = new OrderInfoAdapter();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView =  inflater.inflate(R.layout.fragment_order_manager_tab, container, false);
        mEmptyLayout = mRootView.findViewById(R.id.layout_notice_empty);
        mOrderList = mRootView.findViewById(R.id.layout_order_List);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        mOrderList.setLayoutManager(layoutManager);
        mOrderList.setAdapter(mAdapter);

        executeSearchUserOrderInfo(TAB_ENUM_DISCUSSING);
        return mRootView;
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    private void executeSearchUserOrderInfo(int type){
        HashMap<String, String> header = new HashMap<String, String>();
        header.put("token", AuthController.Instance().getToken());

        HashMap<String, String> param = new HashMap<String, String>();
        param.put("type", String.valueOf(type));

        sendHttpRequest(IHttpRequest.RequestMethod.GET_METHOD,
                RemoteAPI.REQUEST_V2_USER_SR_ORDER,
                header,
                param,
                RemoteAPICmd.REQUEST_V2_USER_SG_ORDER);
    }

    @Override
    protected void onHttpResponse(int actionId, String response) {
        try {
            JSONObject res = new JSONObject(response);
            Gson gson = new Gson();
            if(actionId == RemoteAPICmd.REQUEST_V2_USER_SG_ORDER){
                Log.d(TAG, "onResponse: " + response);
                ResponseInfo responseInfo = gson.fromJson(response, ResponseInfo.class);
                if(responseInfo != null && responseInfo.getStatus() == 1){
                    Type type = new TypeToken<List<OrderStatusInfo>>() {}.getType();
                    mOrderInfo = gson.fromJson(res.getJSONObject("data").getString("list"),
                            type);
                    if(mOrderInfo != null && mOrderInfo.size() > 0) {
                        mEmptyLayout.setVisibility(View.GONE);
                        mAdapter.appendData(mOrderInfo);
                    }
                }
                else{
                    /**/
                }
            }
            else{

            }
        }
        catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onHttpErrorResponse(int actionId, String response) {

    }
}
